package com.zhs.elasticsearch.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @Author zhenghuisheng
 * @Date：2025/8/25 16:40
 */
@Slf4j
public class HttpGet {
    public static void main(String[] args) {
        String url = "https://blog.csdn.net/zhenghuishengq/article/details/150560441?spm=1001.2014.3001.5501";
        try {
            sendGet(url);
            System.out.println("访问成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendGet(String urlStr) {
        try{
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);

            // 设置常见请求头，降低 403 概率
            conn.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                            + "AppleWebKit/537.36 (KHTML, like Gecko) "
                            + "Chrome/120.0.0.0 Safari/537.36");
            conn.setRequestProperty("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");

            int code = conn.getResponseCode();
        }catch (Exception e){
            log.error("请求出现异常{0}", e);
        }
    }
}
